package com.team.weigou.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.team.weigou.R;
import com.team.weigou.adapter.HotRecommenAdapter;
import com.team.weigou.bean.DataEntity;
import com.team.weigou.config.UrlConfig;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Wangs on 2015/7/8.
 */
    public class HotSearchActivity extends Activity implements AdapterView.OnItemClickListener{

    private String name;
    private String pid;
    private TextView textView;
    private HttpUtils httpUtils = new HttpUtils();
    private HotRecommenAdapter hotRecommenAdapter;
    private ArrayList<DataEntity> list = new ArrayList<>();
    private BitmapUtils bitmapUtils;
    private GridView gridView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hotsearch_layout);
        initView();

        Intent intent = this.getIntent();
        name = intent.getStringExtra("name");
        textView.setText(name);
        pid = intent.getStringExtra("pid");
        initBitmUtils();
        gridView.setOnItemClickListener(this);
        hotRecommenAdapter = new HotRecommenAdapter(this, list, bitmapUtils);
        gridView.setAdapter(hotRecommenAdapter);
        initData();
    }

    private void initBitmUtils() {
        bitmapUtils = new BitmapUtils(this);
        bitmapUtils.configDefaultLoadFailedImage(R.mipmap.ic_empty);
        bitmapUtils.configDefaultLoadingImage(R.mipmap.ic_empty);
    }

    private void initData() {
        RequestParams params = getRequestParamR();
        httpUtils.send(HttpRequest.HttpMethod.POST, UrlConfig.HOT_SEARCH_URL, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String jsonResult = responseInfo.result;
                try {
                    String msg = new JSONObject(jsonResult).optString("msg");
                    if ("获取成功".equals(msg)) {
                        JSONArray arr = new JSONObject(jsonResult).getJSONArray("data");
                        List<DataEntity> datas = JSON.parseArray(arr.toString(), DataEntity.class);
                        list.addAll(datas);
                        hotRecommenAdapter.notifyDataSetChanged();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }


    private void initView() {
        textView = (TextView) findViewById(R.id.hot_search_name);
        gridView = (GridView) findViewById(R.id.hot_RS_gradView_second);
    }

    private RequestParams getRequestParamR() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("pageSize", "28");
        params.addBodyParameter("pageNo", "1");
        params.addBodyParameter("pid", pid);
        return params;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intents = new Intent(HotSearchActivity.this, HotInfoActivity.class);
        intents.putExtra("name", list.get(position).getName());
        startActivity(intents);
    }

    public void back(View v){
        this.finish();
    }
}
